class DamageJudgeController < ApplicationController
  def index
    @order_item = get_order_item
    if request.get?
      @position = get_position
      car_size = get_order.car_size
      @spoiler_select = false
      case car_size
        when "軽自動車"
        @spoiler_select = @position.spoiler_mini
        when "普通車"
        @spoiler_select = @position.spoiler_standard
        when "大型車"
        @spoiler_select = @position.spoiler_large
      end
    end
    
    if request.post?
      @order_item.task_type = "本体"
      @order_item.uninstall_index = 0.0
      @order_item.assemble = 0.0
      #session[:spoiler] is saved for the use in get_exchange_index 
      session[:spoiler] = nil
      session[:spoiler] = params[:spoiler]
      @order_item.spoiler = params[:spoiler]
      @order_item.disassemble = params[:disassemble]
      if params[:spoiler] == "T" || params[:disassemble] == "T"
        @order_item.uninstall_index = 0.33
        @order_item.assemble = 0.66
        @order_item.task_type = "単体"
      end
      @order_item.damage_shape = params[:damage_shape]
      @order_item.chap = params[:chap]
      @order_item.hollow = params[:hollow]
      @order_item.curve = params[:curve]
      if params[:chap] == "大" || params[:hollow] == "大" || params[:curve] == "あり"
        @order_item.damage_degree = "樹脂部品破損大"
        @order_item.frame_amend_index = 0.0
        redirect_to :controller => :damage_judge2,:action => "break" and return
        #redirect_to :controller => :damage_judge3 and return
      end
      if params[:damage_shape] == "線状"
        redirect_to :controller => :damage_judge1
      elsif params[:damage_shape] == "面状"
        redirect_to :controller => :damage_judge2
      end
      
    end
    
  end
  #计算最终结果
  def get_result
    order = get_order 
    @order_item = get_order_item
    @position = get_position
    @order_item.get_putty_index(order)
    @order_item.get_paint_index(@position,order)
    @order_item.calculation
  end
  #保存order和order_item对象
  def save_order_item
    position = get_position
    order_item = get_order_item
    order = get_order
    order_item.position = position
    order.order_items << order_item
    if order.save 
      redirect_to :controller => :admin, :action => :show_info
    end
  end
  #只要不进行钣金的，metal_shape设为--，frame设为零
  def get_metal_index
    order_item = get_order_item
    metal_difficulty = 1.0
    
    if params[:difficulty_judge] == "true"
      redirect_to :action => :get_metal_shape and return
    end
    order_item.get_metal_index(metal_difficulty)
    order_item.metal_shape = "なし"
    redirect_to :action => :get_result
  end
  
  def get_metal_shape
    @order_item = get_order_item
    if request.post? 
      metal_difficulty = 1.0
      @order_item.metal_shape = params[:metal_shape]
      if params[:metal_shape] == "アール" || params[:metal_shape] == "ライン"
        metal_difficulty = 1.2
      elsif params[:metal_shape] == "アール＋ライン"
        metal_difficulty = 1.3
      end
      @order_item.get_metal_index(metal_difficulty)
      redirect_to :action => :get_result
    end
  end
end
